/*
 * Copyright (C) 2001-2008  Nishanth Menon <menon.nishanth at gmail dot com>
 *
 * Part of the predict two project done during NITC Final year.
 * Predict2: http://code.google.com/p/predict2/
 * NITC: http://web.nitc.ac.in/~cse/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */


/*
 * MerchantFillupForm.java
 *
 * Created on September 25, 2001, 5:41 PM
 */
package GUI;

import utils.crypto.*;

/**
 * This is to create Enter the Security Information necessary.
 * Reads the USer name and the password and returns the hash of the password
 * @author  lazarus
 * @version 
 */
public class SecurityEntryForm extends javax.swing.JDialog
{

    /** Creates new form MerchantFillupForm */
    public SecurityEntryForm (java.awt.Frame parent, boolean modal, String HashType)
    {
        super (parent, modal);
        initComponents ();
        pack ();
        this.HashType = HashType;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents ()
    {                           // GEN-BEGIN:initComponents
        // setResizable (false);
        FormPanel = new java.awt.Panel ();
        OkayButton = new javax.swing.JButton ();
        QuitButton = new javax.swing.JButton ();
        UserNameEntry = new javax.swing.JTextField ();
        PasswordEntry = new javax.swing.JPasswordField ();
        UserNameLabel = new javax.swing.JLabel ();
        PassPhraseLabel = new javax.swing.JLabel ();
        SecurityHeaderLabel = new javax.swing.JLabel ();
        getContentPane ().setLayout (new javax.swing.BoxLayout (getContentPane (), 0));
        setName ("SecurityEntryForm");
        setTitle ("The Security Entry Form");
        addWindowListener (new java.awt.event.WindowAdapter ()
                           {
                           public void windowClosing (java.awt.event.WindowEvent evt)
                           {
                           closeDialog (evt);
                           }
                           }
        );

        FormPanel.setLayout (null);
        FormPanel.setFont (new java.awt.Font ("Dialog", 0, 11));
        FormPanel.setName ("FormPanel");
        FormPanel.setBackground (java.awt.Color.black);
        FormPanel.setForeground (java.awt.Color.black);

        OkayButton.
            setToolTipText ("Click Here to Enter the Information for Further transactions");
        OkayButton.setLabel ("Okay");
        OkayButton.setBorder (new javax.swing.border.BevelBorder (0));
        OkayButton.setName ("OkayButton");
        OkayButton.setForeground (java.awt.Color.white);
        OkayButton.setActionCommand ("OkayButton");
        OkayButton.setFont (new java.awt.Font ("Arial", 3, 14));
        OkayButton.setText ("Okay");
        OkayButton.setBackground (java.awt.Color.darkGray);
        OkayButton.addMouseListener (new java.awt.event.MouseAdapter ()
                                     {
                                     public void mouseClicked (java.awt.event.MouseEvent evt)
                                     {
                                     OkayButtonMouseClicked (evt);
                                     }
                                     }
        );
        FormPanel.add (OkayButton);
        OkayButton.setBounds (350, 150, 90, 30);


        QuitButton.setBorder (new javax.swing.border.BevelBorder (0));
        QuitButton.setName ("Quit");
        QuitButton.setForeground (java.awt.Color.lightGray);
        QuitButton.setActionCommand ("QuitButton");
        QuitButton.setFont (new java.awt.Font ("Arial", 1, 14));
        QuitButton.setText ("Quit");
        QuitButton.setBackground (java.awt.Color.darkGray);
        QuitButton.addMouseListener (new java.awt.event.MouseAdapter ()
                                     {
                                     public void mouseClicked (java.awt.event.MouseEvent evt)
                                     {
                                     QuitButtonMouseClicked (evt);
                                     }
                                     }
        );
        FormPanel.add (QuitButton);
        QuitButton.setBounds (10, 150, 100, 30);


        UserNameEntry.setBorder (new javax.swing.border.BevelBorder (0));
        UserNameEntry.setCaretColor (java.awt.Color.red);
        UserNameEntry.setForeground (java.awt.Color.cyan);
        UserNameEntry.setFont (new java.awt.Font ("TimesNewRoman", 1, 14));
        UserNameEntry.setBackground (java.awt.Color.gray);
        FormPanel.add (UserNameEntry);
        UserNameEntry.setBounds (300, 60, 150, 20);


        PasswordEntry.setToolTipText ("Please Enter Your PassPhrase Here");
        PasswordEntry.setBorder (new javax.swing.border.BevelBorder (0));
        PasswordEntry.setCaretColor (java.awt.Color.red);
        PasswordEntry.setSelectedTextColor (java.awt.Color.blue);
        PasswordEntry.setFont (new java.awt.Font ("TimesNewRoman", 3, 14));
        PasswordEntry.setBackground (java.awt.Color.gray);
        FormPanel.add (PasswordEntry);
        PasswordEntry.setBounds (300, 100, 150, 20);


        UserNameLabel.setText ("Enter your UserName");
        UserNameLabel.setForeground (java.awt.Color.orange);
        FormPanel.add (UserNameLabel);
        UserNameLabel.setBounds (80, 70, 200, 20);


        PassPhraseLabel.setText ("Please Enter your PassPhrase Here");
        PassPhraseLabel.setForeground (java.awt.Color.orange);
        FormPanel.add (PassPhraseLabel);
        PassPhraseLabel.setBounds (10, 100, 270, 30);


        SecurityHeaderLabel.setBorder (new javax.swing.border.EtchedBorder ());
        SecurityHeaderLabel.setText ("PREDICT II SECURITY ENTRY FORM");
        SecurityHeaderLabel.setForeground (java.awt.Color.red);
        SecurityHeaderLabel.setBackground (java.awt.Color.darkGray);
        SecurityHeaderLabel.setHorizontalAlignment (javax.swing.SwingConstants.CENTER);
        SecurityHeaderLabel.setFont (new java.awt.Font ("Gothic", 0, 12));
        FormPanel.add (SecurityHeaderLabel);
        SecurityHeaderLabel.setBounds (20, 20, 430, 20);


        getContentPane ().add (FormPanel);

        pack ();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit ().getScreenSize ();
        java.awt.Dimension dialogSize = getSize ();
        setSize (new java.awt.Dimension (300, 200));
        setLocation ((screenSize.width - 300) / 2, (screenSize.height - 200) / 2);
    }                           // GEN-END:initComponents

    private void QuitButtonMouseClicked (java.awt.event.MouseEvent evt)
    {                           // GEN-FIRST:event_QuitButtonMouseClicked
        // Add your handling code here:
        System.exit (1);
    }                           // GEN-LAST:event_QuitButtonMouseClicked

    private void OkayButtonMouseClicked (java.awt.event.MouseEvent evt)
    {                           // GEN-FIRST:event_OkayButtonMouseClicked
        // Add your handling code here:
        try
        {
            if (UserNameEntry.getText ().trim ().length () == 0)
                throw (new Exception ("Null value entered"));
            UserName = UserNameEntry.getText ().trim ();
        }
        catch (Exception e)
        {
            UserNameEntry.setText ("");
            UserName = null;
            return;
        }
        try
        {
            if (PasswordEntry.getText ().trim ().length () == 0)
                throw (new Exception ("null Value in Entry"));
        }
        catch (Exception e)
        {
            PasswordEntry.setText ("");
            UserName = null;
            passPhrase = null;
            return;
        }
        // Hash The PassPhrase
        try
        {
            passPhrase = hash.hash (PasswordEntry.getText ().trim (), HashType);
        }
        catch (Exception e)
        {
            System.err.println ("Sorry Unable to Hash PassPhrase due to -" + e + "\n Exiting");
            System.exit (1);
        }
        System.out.println ("UserName=" + UserName + "PassPhrase=" + passPhrase);
        closeDialog (null);
    }                           // GEN-LAST:event_OkayButtonMouseClicked

    /** Closes the dialog */
    private void closeDialog (java.awt.event.WindowEvent evt)
    {                           // GEN-FIRST:event_closeDialog
        setVisible (false);
        dispose ();
    }                           // GEN-LAST:event_closeDialog

    /**
    * @param args the command line arguments
    */
    /* 
     * public static void main (String args[]) { SecurityEntryForm SomeIdiot =
     * new SecurityEntryForm (new javax.swing.JFrame (), true, "SHA1");
     * SomeIdiot.setSize (470, 220); SomeIdiot.setLocation (200, 300);
     * SomeIdiot.show (); } */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Panel FormPanel;
    private javax.swing.JButton OkayButton;
    private javax.swing.JButton QuitButton;
    private javax.swing.JTextField UserNameEntry;
    private javax.swing.JPasswordField PasswordEntry;
    private javax.swing.JLabel UserNameLabel;
    private javax.swing.JLabel PassPhraseLabel;
    private javax.swing.JLabel SecurityHeaderLabel;
    private String HashType;

    // End of variables declaration//GEN-END:variables
    public String passPhrase;
    public String UserName;

}
